package com.boss.salary.service.impl;

import com.boss.common.util.DateUtils;
import com.boss.salary.dao.ISalPostStandardSalaryDao;
import com.boss.salary.dao.ISalaryBasedataDao;
import com.boss.salary.dao.ISalaryMonthEndDao;
import com.boss.salary.entity.SalPostStandardSalary;
import com.boss.salary.entity.SalaryBasedata;
import com.boss.salary.entity.SalaryMonthEnd;
import com.boss.salary.service.IFormulaManageService;
import com.boss.salary.service.ISalPostStandardSalaryService;
import com.boss.salary.vo.SalaryMonthEndVO;
import com.boss.sys.utils.SysUtils;
import com.singularsys.jep.JepException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SalPostStandardSalaryServiceImpl implements ISalPostStandardSalaryService {

    @Autowired
    private ISalPostStandardSalaryDao iSalPostStandardSalaryDao;

    @Autowired
    private IFormulaManageService iFormulaManageService;

    @Autowired
    private ISalaryMonthEndDao iSalaryMonthEndDao;

    @Autowired
    private ISalaryBasedataDao iSalaryBasedataDao;

    @Override
    public List<Map<String, Object>> querySalPostStandardSalaryAll(Map<String, Object> queryParam) {
        return iSalPostStandardSalaryDao.querySalPostStandardSalaryAll(queryParam);
    }
    @Override
    public List<Map<String, Object>> getSalByGradeLevelId(Map<String, Object> queryParam) {
        return iSalPostStandardSalaryDao.getSalByGradeLevelId(queryParam);
    }

    @Override
    public int checkRepeat(Map<String, String> queryParams) {
        return iSalPostStandardSalaryDao.checkRepeat(queryParams);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSalPostStandardSalary(SalPostStandardSalary salPostStandardSalary) {
        salPostStandardSalary.setId(SysUtils.generateGuid());
        salPostStandardSalary.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salPostStandardSalary.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        iSalPostStandardSalaryDao.insertSalPostStandardSalary(salPostStandardSalary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSalPostStandardSalary(SalPostStandardSalary salPostStandardSalary) throws InvocationTargetException, NoSuchMethodException, JepException, IllegalAccessException {
        salPostStandardSalary.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        iSalPostStandardSalaryDao.updateSalPostStandardSalary(salPostStandardSalary);
        calculationSalary(salPostStandardSalary);
    }

    /**
     * 计算工资
     * @param salPostStandardSalary
     * @throws NoSuchMethodException
     * @throws JepException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private void calculationSalary(SalPostStandardSalary salPostStandardSalary) throws NoSuchMethodException, JepException, IllegalAccessException, InvocationTargetException {
        // 获取工资
        List<SalaryMonthEnd> salaryList = iSalPostStandardSalaryDao.queryPersonSalary(salPostStandardSalary);
        // 获取字段值
        SalaryBasedata salaryBasedata = new SalaryBasedata();
        salaryBasedata.setId(salPostStandardSalary.getSalaryId());
        salaryBasedata.setMofDivCode(salPostStandardSalary.getMofDivCode());
        SalaryBasedata salaryBasedata1 = iSalaryBasedataDao.selectById(salaryBasedata);
        StringBuilder sb = new StringBuilder();
        for (int i= 1; i<=150; i++) {
            sb.append("s"+i).append(",");
        }
        for (SalaryMonthEnd salaryMonthEnd: salaryList) {
            BigDecimal gradeMoney = BigDecimal.valueOf(Double.parseDouble(salPostStandardSalary.getSalaryMoney().toString()));
            String salaryCode = salaryBasedata1.getSalaryCode();
            Method setMethod = salaryMonthEnd.getClass().getMethod("set" + salaryCode.substring(0, 1).toUpperCase() + salaryCode.substring(1), Double.class);
            setMethod.invoke(salaryMonthEnd, gradeMoney.doubleValue());
            List<String> agencyIds = new ArrayList<>();
            agencyIds.add(salaryMonthEnd.getAgencyId());
            List<SalaryMonthEndVO> salaryMonthEndList = new ArrayList<>();
            SalaryMonthEndVO salaryMonthEndVO = new SalaryMonthEndVO();
            BeanUtils.copyProperties(salaryMonthEnd, salaryMonthEndVO);
            salaryMonthEndList.add(salaryMonthEndVO);
            List<SalaryMonthEndVO> calculateList = iFormulaManageService.calculateByFormula(salPostStandardSalary.getMofDivCode(), agencyIds, salaryMonthEndList);
            for (SalaryMonthEndVO salaryMonthEndVO1: calculateList) {
                SalaryMonthEnd salaryMonthEnd1 = new SalaryMonthEnd();
                BeanUtils.copyProperties(salaryMonthEndVO1, salaryMonthEnd1);
                salaryMonthEnd1.setColumnNames(sb.toString());
                iSalaryMonthEndDao.updateSalaryMonthEndDao(salaryMonthEnd1);
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalPostStandardSalary(String id) {
        iSalPostStandardSalaryDao.deleteSalPostStandardSalary(id);
    }

}
